﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace ZkSurvey.DAL.SurveyMgr
{
    public class ExternalUserDa
    {
        public static void InsertExternalUser(ExternalUser extUser)
        {
            extUser.RegisterTime = DateTime.Now;
            InsertExternalUser(extUser, DBHelper.GetDatabase(), null);
        }
        public static void InsertExternalUser(ExternalUser extUser, TranscationHelper trans)
        {
            InsertExternalUser(extUser, trans.DataBase, trans.Transaction);
        }
        private static void InsertExternalUser(ExternalUser extUser, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
               extUser.Name,
               extUser.Department,
               extUser.Phone,
               extUser.Email,
               extUser.Address,
               extUser.RegisterTime
            };

            try
            {
                if (trans == null)
                    extUser.Id = Convert.ToInt32(db.ExecuteScalar("Usp_InsertExternalUser", parameters));
                else
                    extUser.Id = Convert.ToInt32(db.ExecuteScalar(trans, "Usp_InsertExternalUser", parameters));
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static ExternalUser GetExternalUser(int id)
        {
            Database db = DBHelper.GetDatabase();
            ExternalUser extUser = null;

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetExternalUser", id))
            {
                if (dataReader.Read())
                {
                    extUser = PopulateExternalUser(dataReader);
                }
            }
            return extUser;
        }

        public static List<ExternalUser> GetExternalUserList()
        {
            Database db = DBHelper.GetDatabase();
            List<ExternalUser> extUsers = new List<ExternalUser>();
            ExternalUser extUser = null;

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetExternalUserList"))
            {
                while (dataReader.Read())
                {
                    extUsers.Add(PopulateExternalUser(dataReader));
                }
            }
            return extUsers;
        }

        private static ExternalUser PopulateExternalUser(IDataReader dataReader)
        {
            ExternalUser extUser = new ExternalUser();
            extUser.Id = dataReader.GetInt32(0);
            extUser.Name = dataReader.GetString(1);
            if (!dataReader.IsDBNull(2))
                extUser.Department = dataReader.GetString(2);
            extUser.Phone = dataReader.GetString(3);
            extUser.Email = dataReader.GetString(4);
            if (!dataReader.IsDBNull(5))
                extUser.Address = dataReader.GetString(5);
            extUser.RegisterTime = dataReader.GetDateTime(6);

            return extUser;
        }
    }
}
